package com.vitabuybuy.tv.ui.mine.account.viewmodel

import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.HomeRepository

class SmsVerifyViewModel : BaseViewModel() {

    fun sendToPhone(phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.getCodeV2(phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun sendToPhoneV2(phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.getCode(phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun validateByPhone(phone: String, code: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.checkPhoneAndCode(code, phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetPhone(phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.resetPhone(phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
